package com.thinkgem.jeesite.httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.thinkgem.jeesite.common.utils.HttpClientUtil;  
  
public class Wordpress{  
    public static void main(String []args) throws Exception{  
        System.out.println("start");  
          
        try {                 
            String url="http://quhongye.cn/wp-login.php";  
                          
            List<NameValuePair> pairs=new ArrayList<NameValuePair>();  
            pairs.add(new BasicNameValuePair("log","admin"));  
            pairs.add(new BasicNameValuePair("pwd","WCadminyier34"));  
              
            HttpClient httpclient=new DefaultHttpClient();  
            HttpPost request=new HttpPost(url);  
            request.setEntity(new UrlEncodedFormEntity(pairs));           
            HttpResponse response=httpclient.execute(request);  
            
            System.out.println("====>" + HttpClientUtil.httpGetRequest("http://quhongye.cn/wp-admin/edit.php"));
            Header[] headers=response.getAllHeaders();  
            StringBuilder sb=new StringBuilder();  
            for(Header header:headers){  
                if(header.getName().equals("Set-Cookie")){  
                    String cookiestr=header.getValue();  
                    int pos=cookiestr.indexOf("=");  
                    String name=cookiestr.substring(0, pos);  
                    String value=cookiestr.substring(pos+1, cookiestr.indexOf(";"));  
                    sb.append(name+"="+value+";");  
                }                 
            }  
            EntityUtils.consume(response.getEntity());  
                          
            String url2="http://quhongye.cn/wp-admin/post-new.php";  
            HttpGet request2=new HttpGet(url2);  
            request2.setHeader("Cookie", sb.toString());  
            response=httpclient.execute(request2);  
            InputStream in=response.getEntity().getContent();  
            BufferedReader br=new BufferedReader(new InputStreamReader(in));  
            String line="";  
            String _wpnonce = null;  
            while((line=br.readLine())!=null){  
                if(line.indexOf("name=\"_wpnonce\"")>=0){  
                    System.out.println(line);  
                    int pos=line.indexOf("value");  
                    _wpnonce=line.substring(pos+7, pos+17);  
                    System.out.println(_wpnonce);  
                }  
            }  
            EntityUtils.consume(response.getEntity());  
              
            String postURL="http://quhongye.cn/wp-admin/post.php";            
            List<NameValuePair> params=new ArrayList<NameValuePair>();  
                          
            params.add(new BasicNameValuePair("_wpnonce",_wpnonce));  
            params.add(new BasicNameValuePair("action","editpost"));              
            params.add(new BasicNameValuePair("post_ID","91"));  
            params.add(new BasicNameValuePair("post_type","post"));  
            params.add(new BasicNameValuePair("post_title","new title from httpclient"));  
            params.add(new BasicNameValuePair("content","new content from httpclient"));  
            params.add(new BasicNameValuePair("comment_status","open"));  
            params.add(new BasicNameValuePair("post_author","1"));  
            params.add(new BasicNameValuePair("user_ID","1"));  
            params.add(new BasicNameValuePair("publish","发布"));//这个必须有，否则就成草稿了            
              
            HttpPost postrequest=new HttpPost(postURL);  
            postrequest.setHeader("Cookie", sb.toString());  
            postrequest.setEntity(new UrlEncodedFormEntity(params));  
            response=httpclient.execute(postrequest);  
              
            System.out.println(response.getStatusLine());  
            headers=response.getAllHeaders();  
            for(Header header:headers){  
                System.out.println(header);  
            }  
            System.out.println(EntityUtils.toString(response.getEntity()));  
  
        } catch (IOException e) {  
        	
            System.out.println("failed");  
              e.printStackTrace();
        }  
    }  
}  
